<script setup>

import Dialog from "@/components/dialog/Dialog.vue";
import {inject, onMounted, ref, watch} from "vue";

const props = defineProps({
    imageConfigDialog: {
        type: Object,
        default: () => {
            return {
                data: {

                }
            }
        }
    },
    fineTune: {
        type: Boolean,
        default: false
    },
    models: {
        type: Array,
        default: []
    },
    defaultConfig: {
        type: Object,
        default: () => {
            return {
                data: {

                }
            }
        }
    }
})
const message = inject('message')
const options = [
    {
        value: 0,
        label: '无正则模式'
    },
    {
        value: 1,
        label: 'L1正则模式'
    },
    {
        value: 2,
        label: 'L2正则模式'
    }
]

const emits = defineEmits([
    'onTraining',// 页数改变事件
])

const onTraining = () => {
    if(!props.imageConfigDialog.data?.remark){
        message.warning("输入模型名称")
        return
    }
    emits('onTraining')
}

const onModelChange = (e) => {
    props.models.forEach(item => {
        if(item.remark !== e){
            return
        }
        props.imageConfigDialog.data = item.modelConfig
        props.imageConfigDialog.data.remark = e
    })
}

const onCancel = (e) => {
    props.imageConfigDialog.data = props.defaultConfig
    props.imageConfigDialog.data.remark = null
}

onMounted(() => {
})
</script>

<template>
    <Dialog :dialog="imageConfigDialog">
        <el-form :model="imageConfigDialog.data" label-width="auto" :label-position="'top'" style="width: 100%">
            <el-form-item label="是否打印日志">
                <el-switch v-model="imageConfigDialog.data.showLog" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="检测窗宽度，在标注框0.6-1.5倍之间" >
                <el-input-number v-model="imageConfigDialog.data.windowWidth" :min="1" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="检测窗高度，在标注框0.6-1.5倍之间" >
                <el-input-number v-model="imageConfigDialog.data.windowHeight" :min="1" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="神经网络线性层，隐层神经元数量">
                <el-input-number v-model="imageConfigDialog.data.hiddenNerveNub" :min="0" :disabled="fineTune"  style="width: 100%;" />
            </el-form-item>
            <el-form-item label="神经网络卷积层，卷积核大小">
                <el-input-number v-model="imageConfigDialog.data.kernelSize" :min="0" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="图像学习率">
                <el-input-number v-model="imageConfigDialog.data.studyRate" :min="0" :max="1"  style="width: 100%;" />
            </el-form-item>
            <el-form-item label="图像1v1卷积层学习率">
                <el-input-number v-model="imageConfigDialog.data.oneConvStudy" :min="0" :max="1" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="训练次数，控制在 样本数量 * 训练次数 = [1000~2000]，当设置检测框越大，则需要样本数量越多">
                <el-input-number v-model="imageConfigDialog.data.enhance" :min="1" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="满足交并比阈值后会得到多个预测框，预测框之间两两比较 < 阈值，删除预测框">
                <el-input-number v-model="imageConfigDialog.data.iouTh" :min="0" :max="1" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="交并比阈值，检测框与标注框相交面积 / 标注框自身面积 > 阈值，认为可以训练">
                <el-input-number v-model="imageConfigDialog.data.containIouTh" :min="0" :max="1" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="加速线程的数量">
                <el-input-number v-model="imageConfigDialog.data.coreNumber" :min="0" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="[0~1]，识别中会为每个检测框打分，当低于这个指的时候认为是不需要检测">
                <el-input-number v-model="imageConfigDialog.data.pth" :min="0" :max="1" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="训练时窗口移动步长">
                <el-input-number v-model="imageConfigDialog.data.stepReduce" :min="0.1" :max="1" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="检测时窗口移动步长">
                <el-input-number v-model="imageConfigDialog.data.checkStepReduce" :min="0.1" :max="1" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="升降维度通道数，通道越低速度越快，越适合更简单的识别场景。通道数越大速度越慢，越能适合更复杂识别场景，该值最小为1">
                <el-input-number v-model="imageConfigDialog.data.channelNo" :min="1" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="是否进行维度调节，大多时候为默认值为true，当它为false的时候则不进行通道的升降维度，如果样本量较少，可以增加误差的下降速度，样本需求量降低，但是也会损失些许精度">
                <el-switch style="width: 200px"
                    v-model="imageConfigDialog.data.norm"
                    inline-prompt
                    active-text="是"
                    inactive-text="否"
                />
            </el-form-item>
            <el-form-item label="卷积层最后输出矩阵特征最小维度">
                <el-input-number v-model="imageConfigDialog.data.minFeatureValue" :min="0" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="自适应学习率,打开时，则学习率会在学习中自适应梯度">
                <el-switch v-model="imageConfigDialog.data.auto" :active-value="true" :inactive-value="false" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="自适应学习率的衰减系数,常为0.9" v-if="imageConfigDialog.data.auto">
                <el-input-number v-model="imageConfigDialog.data.gaMa" :min="0" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="梯度裁剪阈值,当梯度超过该阈值时,该梯度会被裁剪,防止突发性大梯度,引起梯度爆炸" >
                <el-input-number v-model="imageConfigDialog.data.gmaxTh" :min="0" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="是否用概率作为可信度,默认关闭" >
                <el-switch v-model="imageConfigDialog.data.proTrust" :active-value="true" :inactive-value="false" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="正则系数">
                <el-input-number v-model="imageConfigDialog.data.regular" :min="0" :max="1" :disabled="fineTune" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="正则模式" >
                <el-select v-model="imageConfigDialog.data.regularModel" :disabled="fineTune">
                    <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
                <el-text v-show="imageConfigDialog.data.rzModel === 0">
                    无正则模式即不对权重施加惩罚，或者样本数据量较为大充足时，样本规律性较强，可直接使用无正则模式
                </el-text>
                <el-text v-show="imageConfigDialog.data.rzModel === 1">
                    L1正则模式，当出现异常值，或者未见过的异常噪音时，L1的鲁棒性非常强。它是以简化模型，突出重点权重的方式来增强其稳定性与鲁棒性的
                </el-text>
                <el-text v-show="imageConfigDialog.data.rzModel === 2">
                    L2正则模式，它更容易调动全部参数特征，让特征平均的落在每个权重上，误差最容易最快下降到最优解的位置上。面对形态多变且种类复杂，或者维度比较大的情况下，能获得最好的优化效果
                </el-text>
            </el-form-item>

        </el-form>
        <template #table-dialog-btn>
            <el-select
                :disabled="fineTune"
                v-model="imageConfigDialog.data.remark"
                filterable
                allow-create
                default-first-option
                clearable
                :reserve-keyword="false"
                placeholder="模型名称"
                style="width: 200px;margin-right: 10px"
                @change="onModelChange"
                @clear="onCancel"
            >
                <el-option
                    v-for="item in models"
                    :key="item.remark"
                    :label="item.remark"
                    :value="item.remark"
                />
            </el-select>
            <el-button type="primary" @click="onTraining">训练</el-button>
        </template>
    </Dialog>
</template>

<style scoped lang="scss">
:deep(.el-text){
    line-height: 1.2;
}
</style>